package com.wujian.love.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.convert.Convert;
import com.wujian.love.config.RedisConfig;
import com.wujian.love.domin.CustomerAccount;
import com.wujian.love.mapper.CustomerAccountMapper;
import com.wujian.love.service.ICustomerAccountService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户 服务层实现
 * 
 * @author wujian
 * @date 2020-06-17
 */
@Slf4j
@Service
public class CustomerAccountServiceImpl implements ICustomerAccountService
{
	@Autowired
	private CustomerAccountMapper customerAccountMapper;

	@Autowired
	private RedisConfig redisConfig;

	/**
     * 查询用户信息
     * 
     * @param id 用户ID
     * @return 用户信息
     */
    @Override
	public CustomerAccount selectCustomerAccountById(Integer id)
	{
	    return customerAccountMapper.selectCustomerAccountById(id);
	}
	
	/**
     * 查询用户列表
     * 
     * @param customerAccount 用户信息
     * @return 用户集合
     */
	@Override
	public List<CustomerAccount> selectCustomerAccountList(CustomerAccount customerAccount)
	{
	    return customerAccountMapper.selectCustomerAccountList(customerAccount);
	}
	
    /**
     * 新增用户
     * 
     * @param customerAccount 用户信息
     * @return 结果
     */
	@Override
	public int insertCustomerAccount(CustomerAccount customerAccount)
	{
	    return customerAccountMapper.insertCustomerAccount(customerAccount);
	}
	
	/**
     * 修改用户
     * 
     * @param customerAccount 用户信息
     * @return 结果
     */
	@Override
	public int updateCustomerAccount(CustomerAccount customerAccount)
	{
	    return customerAccountMapper.updateCustomerAccount(customerAccount);
	}

	/**
     * 删除用户对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteCustomerAccountByIds(String ids)
	{
		return customerAccountMapper.deleteCustomerAccountByIds(Convert.toStrArray(ids));
	}

	/**
	 * 测试多线程环境下金额
	 * @return
	 */
	@Override
    public int testLockBanlance() {
		//RedissonClient redissonClient = redisConfig.redissonClient();
		//RLock lock = redissonClient.getLock("test-lockBanlance");
		int testNumber = 0;
//		//并发处理加锁
//		try {
//			/**
//			 * 4.尝试获取锁
//			 * waitTimeout 尝试获取锁的最大等待时间，超过这个值，则认为获取锁失败
//			 * leaseTime   锁的持有时间,超过这个时间锁会自动失效（值应设置为大于业务处理的时间，确保在锁有效期内业务能处理完）
//			 */
//			 boolean res = lock.tryLock(2, 5, TimeUnit.SECONDS);
//			System.out.println("线程:"+Thread.currentThread().getName() + "    :"+res);
//			if(res){
//				CustomerAccount customerAccount = customerAccountMapper.selectCustomerAccountById(1);
//				Integer oldCurrentBalance = customerAccount.getCurrentBalance();
//				int currentBalance = oldCurrentBalance - 1;
//				customerAccount.setCurrentBalance(currentBalance);
//				customerAccountMapper.updateCustomerAccount(customerAccount);
//				testNumber=  customerAccount.getCurrentBalance();
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}finally {
//			if(lock.isLocked()){
//				if(lock.isHeldByCurrentThread()){
//					lock.unlock();
//				}
//			}
//		}
		return testNumber;
	}
}
